package com.chat.controller;


import com.chat.entry.dto.message.MessageDTO;
import com.chat.result.Result;
import com.chat.service.IUserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户聊天表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-21
 */
@Slf4j
@RestController
@Api(tags = "用户聊天接口")
@RequestMapping("/api/user-message")
@RequiredArgsConstructor
public class UserMessageController {
    private final IUserMessageService userMessageService;

    @ApiOperation(value = "发送消息", notes = "通过用户id发送消息")
    @PostMapping("/sendMessage")
    public Result sendMessage(
            @ApiParam(value = "发送消息的内容", required = true) @RequestBody MessageDTO messageDTO
    ) {
        return userMessageService.sendMessage(messageDTO);
    }

    @ApiOperation(value = "获取所有未读消息统计", notes = "返回总未读数、各好友未读数和群聊未读数")
    @GetMapping("/unreadSummary/{receiverId}")
    public Result getUnreadSummary(
            @ApiParam(value = "接受消息用户id", required = true) @PathVariable Integer receiverId
    ) {
        return userMessageService.getUnreadSummary(receiverId);
    }

    @ApiOperation(value = "获取聊天列表", notes = "返回好友聊天和群聊聊天")
    @GetMapping("/list")
    public Result getMessageList(
            @ApiParam(value = "用户id", required = true) @RequestParam Integer userId
    ) {
        return userMessageService.getMessageList(userId);
    }

    @ApiOperation(value = "查询聊天记录", notes = "根据用户id和好友id来查询聊天记录（分页）")
    @GetMapping("/query")
    public Result queryMessage(
            @ApiParam(value = "用户id", required = true) @RequestParam Integer userId,
            @ApiParam(value = "好友id", required = true) @RequestParam Integer friendId,
            @ApiParam(value = "页码（从1开始）", required = false, example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页条数", required = false, example = "20") @RequestParam(defaultValue = "20") Integer pageSize
    ) {
        return userMessageService.queryMessage(userId, friendId, pageNum, pageSize);
    }

    @ApiOperation(value = "根据消息id来查询聊天记录", notes = "根据消息id、用户id、好友id来查询聊天记录（分页）")
    @GetMapping("/queryByContent")
    public Result queryMessageByContent(
            @RequestParam Integer userId,
            @RequestParam Integer friendId,
            @RequestParam Long messageId,
            @RequestParam(defaultValue = "20") Integer pageSize
    ) {
        return userMessageService.queryMessageByContent(userId, friendId, messageId, pageSize);
    }

    @ApiOperation(value = "搜索聊天", notes = "根据输入的内容进行模糊")
    @GetMapping("/list/search")
    public Result searchMessage(
            @ApiParam(value = "用户id", required = true) @RequestParam Integer userId,
            @ApiParam(value = "聊天内容", required = true) @RequestParam String content
    ) {
        return userMessageService.searchMessage(userId, content);
    }
}
